package com.mmcg.service.impl;

import com.mmcg.config.GitEEProperties;
import com.mmcg.service.GiteeService;
import com.ruoyi.common.core.domain.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.Map;

/**
 * 作者：李嘉浩
 * 时间：2024-09-17-16:55
 * 功能：
 */
@Service
public class GiteeServiceImpl implements GiteeService {
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private GitEEProperties gitEEProperties;
    @Override
    public Map<String, String> getUserInfo(String code) {
        //获取access_token
        String url = String.format("https://gitee.com/oauth/token?grant_type=%s&code=%s&client_id=%s&redirect_uri=%s&client_secret=%s",
                gitEEProperties.getGrantType(),code,
                gitEEProperties.getClient_id(),gitEEProperties.getRedirect_uri(),
                gitEEProperties.getClient_secret());
        Map<String,String> map = restTemplate.postForObject(url, null, Map.class);
        String accessToken = map.get("access_token");
        //获取用户信息
        String userUrl = String.format("https://gitee.com/api/v5/user?access_token=%s", accessToken);
        Map<String,String> userMap = restTemplate.getForObject(userUrl, Map.class);
        return userMap;
    }
}
